package cn.edu.ujn.lab3.web;

import cn.edu.ujn.lab3.dao.BaseDict;
import cn.edu.ujn.lab3.dao.Invoice;
import cn.edu.ujn.lab3.dao.Orders;
import cn.edu.ujn.lab3.service.BaseDictService;
import cn.edu.ujn.lab3.service.InvoiceService;
import cn.edu.ujn.lab3.service.OrdersService;
import cn.edu.ujn.lab3.util.Page;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/invoice")
public class InvoiceController {
	
    private final String INVOICE_TYPE = "011";
    
    @Autowired
    private InvoiceService invoiceService;
    
    @Autowired
    private BaseDictService baseDictService;
    
    @Autowired
    private OrdersService ordersService;

    //查询发票列表
    @RequestMapping("/list.action")
    public String list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer rows,
                       Invoice invoice, Model model) {
        invoice.setRows(rows);
        Page<Invoice> invoiceList = invoiceService.findInvoiceList(page, invoice);
        List<BaseDict> dictByTypeCode = baseDictService.findBaseDictByTypeCode(INVOICE_TYPE);
        model.addAttribute("page", invoiceList);
        model.addAttribute("orderId", invoice.getOrderId());
        model.addAttribute("invoiceTitle", invoice.getInvoiceTitle());
        model.addAttribute("invoiceContent", invoice.getInvoiceContent());
        model.addAttribute("invoiceAmount", invoice.getInvoiceAmount());
        model.addAttribute("invoiceType", invoice.getInvoiceType());
        model.addAttribute("dictByTypeCode", dictByTypeCode);
        return "invoice";
    }  
    
    //创建发票
    @RequestMapping("/create.action")
    @ResponseBody
    public String invoiceCreate(Invoice invoice) {
    	// System.out.println(invoice);
        // 订单数据非法
        if (!StringUtils.isNumeric(invoice.getOrderId())) {
            return "fail1";
        }
        Invoice invoice1 = new Invoice();
        invoice1.setRows(10);
        invoice1.setOrderId(invoice.getOrderId());
        Page<Invoice> invoiceList = invoiceService.findInvoiceList(1, invoice1);
        // 发票中订单编号已存在，一个订单不允许开两个发票
        if (!invoiceList.getRows().isEmpty()) {
            return "fail2";
        }
        // 订单没找到的情况
        Orders ordersById = ordersService.getOrdersById(Integer.valueOf(invoice.getOrderId()));
        if (ordersById == null) {
            return "fail3";
        }
        
        int rows = invoiceService.createInvoice(invoice);
        if (rows > 0) {
            return "OK";
        } else {
            return "fail4";
        }
    }
    
    //通过id查询发票
    @RequestMapping("/getInvoiceById.action")
    @ResponseBody
    public Invoice getInvoiceById(Integer id) {
        return invoiceService.getInvoiceById(id);
    }
    
    //更新发票
    @RequestMapping("/update.action")
    @ResponseBody
    public String invoiceUpdate(Invoice invoice) {
        // 订单数据非法
        if (!StringUtils.isNumeric(invoice.getOrderId())) {
            return "fail1";
        }
        // 没找到的情况
        Orders ordersById = ordersService.getOrdersById(Integer.valueOf(invoice.getOrderId()));
        if (ordersById == null) {
            return "fail3";
        }

        int rows = invoiceService.updateInvoice(invoice);
        if (rows > 0) {
            return "OK";
        } else {
            return "fail4";
        }
    }
    //删除发票
    @RequestMapping("/delete.action")
    @ResponseBody
    public String invoiceDelete(Integer id) {
        int rows = invoiceService.deleteInvoice(id);
        if (rows > 0) {
            return "OK";
        } else {
            return "fail4";
        }
    }

}